# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=field3d
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.3
pkgrel=3
pkgdesc="Open source library for storing voxel data (mingw-w64)"
arch=('any')
url="https://sites.google.com/site/field3d/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-openexr")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'buildflags')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/Field3D/archive/v${pkgver}.tar.gz
        001-cmake-mingw.patch
        002-win32-fix-exported-symbols.patch
        005-fix-build-hdf5-1.12.patch)
sha256sums=('b6168bc27abe0f5e9b8d01af7794b3268ae301ac72b753712df93125d51a0fd4'
            'b1ab7061710ebd9334acd3ce6b47c899f4a69c2005204fbe3c8a857c7fe3ea86'
            '087a28c98a48f0ea1451b1a1d34e5fc102caecb45cb1219f7d65df5b18015b26'
            '01fda162386063c4426f18a38dd1d545c5b9609b8c63b322dd7814e76b37bdfa')

prepare() {
  cd ${srcdir}/Field3D-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake-mingw.patch
  patch -p1 -i ${srcdir}/002-win32-fix-exported-symbols.patch
  patch -p1 -i ${srcdir}/005-fix-build-hdf5-1.12.patch
}

build() {
  cd ${srcdir}
  [[ -d shared-${MINGW_CHOST} ]] && rm -r shared-${MINGW_CHOST}
  mkdir shared-${MINGW_CHOST} && cd shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    ../Field3D-${pkgver}
  make
  
  cd ${srcdir}
  [[ -d static-${MINGW_CHOST} ]] && rm -r static-${MINGW_CHOST}
  mkdir static-${MINGW_CHOST} && cd static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    ../Field3D-${pkgver}
  make
}

package() {
  cd "${srcdir}"/shared-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  
  cd "${srcdir}"/static-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  
  install -Dm644 "${srcdir}/Field3D-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
